package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.CouponMy;
 

/**
 * @title   
 * @autho 
 * @date   
 */
@Service
public class CouponMyService extends BaseDao<CouponMy>{

	/**
	 * 按条件查询
	 * @param coupon
	 * @return
	 */
	public List<CouponMy> query(CouponMy coupon){
		if(coupon==null) return super.findAll();
		if(coupon.getiDisplayLength() > 0){
	     return super.find(createFilter(coupon),coupon.getiDisplayStart(),coupon.getiDisplayLength());
		}
		return super.find(createFilter(coupon));
	}
	
	/**
	 * 分页查询
	 * @param coupon
	 * @return
	 */
    public List<CouponMy> querypage(CouponMy coupon){
	       return super.find(createFilter(coupon),coupon.getPageNo(),18);
	}
    
	public Long count(CouponMy coupon){
		return super.count(createFilter(coupon));
	}
	
	/**
	 * 拼装条件
	 * @param coupon
	 * @return 
	 */
	private DetachedCriteria createFilter(CouponMy coupon){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	
		if(!StringUtil.isNullOrEmpty(coupon.getStatus())) {
	        dc.add(Restrictions.eq("status", coupon.getStatus()));
		} 
	  
		dc.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);// 结果集去重
		dc.addOrder(Order.desc("createTime"));
		return dc;
	}
	

}
